var IP = "192.168.105.79";


var table_ =
{
    "jaw": "j_obj",
    "B0101": "j_b_1",
    "B0102": "j_b_2",
    "B0103": "j_b_3",
    "B0104": "j_b_4",
    "B0105": "j_b_5",
    "B0201": "j_t_1",
    "B0202": "j_t_2",
    "B0203": "j_t_3",
    "B0204": "j_t_4",
    "B0205": "j_t_5",
    "B010101": "lb101j",
    "B010102": "lb102j",
    "B010103": "lb103j",
    "B010104": "lb104j",
    "B010105": "lb105j",
    "B010106": "lb106j",
    "B010107": "lb107j",
    "B010201": "lb201j",
    "B010202": "lb202j",
    "B010203": "lb203j",
    "B010204": "lb204j",
    "B010205": "lb205j",
    "B010206": "lb206j",
    "B010207": "lb207j",
    "B010301": "lb301j",
    "B010302": "lb302j",
    "B010303": "lb303j",
    "B010304": "lb304j",
    "B010305": "lb305j",
    "B010306": "lb306j",
    "B010307": "lb307j",
    "B010308": "lb308j",
    "B010309": "lb309j",
    "B010310": "lb310j",
    "B010311": "lb311j",
    "B010312": "lb312j",
    "B010401": "lb401j",
    "B010402": "lb402j",
    "B010403": "lb403j",
    "B010404": "lb404j",
    "B010405": "lb405j",
    "B010406": "lb406j",
    "B010407": "lb407j",
    "B010408": "lb408j",
    "B010409": "lb409j",
    "B010410": "lb410j",
    "B010411": "lb411j",
    "B010412": "lb412j",
    "B010501": "lb501j",
    "B010502": "lb502j",
    "B010503": "lb503j",
    "B010504": "lb504j",
    "B010505": "lb505j",
    "B010506": "lb506j",
    "B010507": "lb507j",

    "B020101": "lt101j",
    "B020102": "lt102j",
    "B020103": "lt103j",
    "B020104": "lt104j",
    "B020105": "lt105j",
    "B020106": "lt106j",
    "B020107": "lt107j",
    "B020201": "lt201j",
    "B020202": "lt202j",
    "B020203": "lt203j",
    "B020204": "lt204j",
    "B020205": "lt205j",
    "B020206": "lt206j",
    "B020207": "lt207j",
    "B020301": "lt301j",
    "B020302": "lt302j",
    "B020303": "lt303j",
    "B020304": "lt304j",
    "B020305": "lt305j",
    "B020306": "lt306j",
    "B020307": "lt307j",
    "B020308": "lt308j",
    "B020309": "lt309j",
    "B020310": "lt310j",
    "B020311": "lt311j",
    "B020312": "lt312j",
    "B020401": "lt401j",
    "B020402": "lt402j",
    "B020403": "lt403j",
    "B020404": "lt404j",
    "B020405": "lt405j",
    "B020406": "lt406j",
    "B020407": "lt407j",
    "B020408": "lt408j",
    "B020409": "lt409j",
    "B020410": "lt410j",
    "B020411": "lt411j",
    "B020412": "lt412j",
    "B020501": "lt501j",
    "B020502": "lt502j",
    "B020503": "lt503j",
    "B020504": "lt504j",
    "B020505": "lt505j",
    "B020506": "lt506j",
    "B020507": "lt507j"
};

var table2_ = {
    "T010101": "lb101j",
    "T010102": "lb102j",
    "T010103": "lb103j",
    "T010104": "lb104j",
    "T010105": "lb105j",
    "T010106": "lb106j",
    "T010107": "lb107j",
    "T010201": "lb201j",
    "T010202": "lb202j",
    "T010203": "lb203j",
    "T010204": "lb204j",
    "T010205": "lb205j",
    "T010206": "lb206j",
    "T010207": "lb207j",
    "T010301": "lb301j",
    "T010302": "lb302j",
    "T010303": "lb303j",
    "T010304": "lb304j",
    "T010305": "lb305j",
    "T010306": "lb306j",
    "T010307": "lb307j",
    "T010308": "lb308j",
    "T010309": "lb309j",
    "T010310": "lb310j",
    "T010311": "lb311j",
    "T010312": "lb312j",
    "T010401": "lb401j",
    "T010402": "lb402j",
    "T010403": "lb403j",
    "T010404": "lb404j",
    "T010405": "lb405j",
    "T010406": "lb406j",
    "T010407": "lb407j",
    "T010408": "lb408j",
    "T010409": "lb409j",
    "T010410": "lb410j",
    "T010411": "lb411j",
    "T010412": "lb412j",
    "T010501": "lb501j",
    "T010502": "lb502j",
    "T010503": "lb503j",
    "T010504": "lb504j",
    "T010505": "lb505j",
    "T010506": "lb506j",
    "T010507": "lb507j",

    "T020101": "lt101j",
    "T020102": "lt102j",
    "T020103": "lt103j",
    "T020104": "lt104j",
    "T020105": "lt105j",
    "T020106": "lt106j",
    "T020107": "lt107j",
    "T020201": "lt201j",
    "T020202": "lt202j",
    "T020203": "lt203j",
    "T020204": "lt204j",
    "T020205": "lt205j",
    "T020206": "lt206j",
    "T020207": "lt207j",
    "T020301": "lt301j",
    "T020302": "lt302j",
    "T020303": "lt303j",
    "T020304": "lt304j",
    "T020305": "lt305j",
    "T020306": "lt306j",
    "T020307": "lt307j",
    "T020308": "lt308j",
    "T020309": "lt309j",
    "T020310": "lt310j",
    "T020311": "lt311j",
    "T020312": "lt312j",
    "T020401": "lt401j",
    "T020402": "lt402j",
    "T020403": "lt403j",
    "T020404": "lt404j",
    "T020405": "lt405j",
    "T020406": "lt406j",
    "T020407": "lt407j",
    "T020408": "lt408j",
    "T020409": "lt409j",
    "T020410": "lt410j",
    "T020411": "lt411j",
    "T020412": "lt412j",
    "T020501": "lt501j",
    "T020502": "lt502j",
    "T020503": "lt503j",
    "T020504": "lt504j",
    "T020505": "lt505j",
    "T020506": "lt506j",
    "T020507": "lt507j"
};

newMaterial1 = new MeshPhongMaterial({
    color: 0xff0000,
    shininess: 100
  });

  newMaterial0 = new MeshPhongMaterial({
    color: 0xeeeeee,
    shininess: 100
  });

  newMaterial2 = new MeshPhongMaterial({
    color: 0x0000ff,
    shininess: 100
  });

  newMaterial3 = new MeshPhongMaterial({
    color: 0xff00ff,
    shininess: 100
  });


var ws = new ReconnectingWebSocket("ws://" + IP + ":1883/ws");

ws.onmessage = function (e) {

    var obj = JSON.parse(e.data);
    for (a in obj) {
        // console.debug(a.indexOf("OP40"));
        if (a.indexOf("OP40_B") != -1) {
            var key_ = a.replace("OP40_B", "");
            try {
                robot.joints[table_["B"+key_]].visible = (obj[a] == 0 ? false : true);
            } catch (ex) {
                //TODO
            }
        }

        if (a.indexOf("OP40_T") != -1) {
            key_ = a.replace("OP40_T","");
            try{
                if (obj[a] == 1 || obj[a]==0) {
                    robot.joints[table2_["T"+key_]].children[0].children[0].children[0].material = newMaterial0;
                }
                else if (obj[a] == 101) {
                    robot.joints[table2_["T"+key_]].children[0].children[0].children[0].material = newMaterial1;
                }
                else if (obj[a] == 102) {
                    robot.joints[table2_["T"+key_]].children[0].children[0].children[0].material = newMaterial2;
                }
                else if (obj[a] == 103) {
                    robot.joints[table2_["T"+key_]].children[0].children[0].children[0].material = newMaterial3;
                }
            }catch (ex) {
                //TODO
            }

        }
    }

};

